﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QRCode2.Models;
using System.Net;
using System.Drawing;
using System.Drawing.Imaging;

namespace QRCode2.Controllers
{
    public class HomeForSaleController : Controller
    {
        private HomeForSaleDBContext db = new HomeForSaleDBContext();
        public HomeQRCodeDBContext dbQR = new HomeQRCodeDBContext();
        public UserDBContext dbUser = new UserDBContext();
        public CompanyDBContext dbCompany = new CompanyDBContext();

        //
        // GET: /HomeForSale/

        [Authorize(Roles = "RealEstateAgent")]
        public ActionResult Index()
        {
            //Return Home for sales only for current Agent except for Admin

            List<HomeForSale> homesForSale = db.HomeForSales.Where(b => b.AgentUserName == User.Identity.Name).ToList();

            return View(homesForSale);
        }

        //
        // GET: /HomeForSale/Details/5

        [Authorize(Roles = "RealEstateAgent")]
        public ActionResult Details(int id = 0)
        {
            HomeForSale homeforsale = db.HomeForSales.Find(id);
            if (homeforsale == null)
            {
                return HttpNotFound();
            }

            string MainPictureLocation = "";
            string Picture1Location = "";
            string Picture2Location = "";
            string Picture3Location = "";
            string Picture4Location = "";
            string Picture5Location = "";
            string MovieLocation = "";

            if (homeforsale.MainPicture != null)
            {
                MainPictureLocation = ("~/Images/HomeForSale/Picture/") + homeforsale.MainPicture.ToString();
            }
            else
            {
                MainPictureLocation = ("~/Images/HomeForSale/Picture/bullet.png");
            }

            ViewBag.MainPicture = MainPictureLocation;

            if (homeforsale.Picture1 != null)
            {
                Picture1Location = ("~/Images/HomeForSale/Picture/") + homeforsale.Picture1.ToString();
            }
            else
            {
                Picture1Location = ("~/Images/HomeForSale/Picture/bullet.png");
            }

            ViewBag.Picture1 = Picture1Location;

            if (homeforsale.Picture2 != null)
            {
                Picture2Location = ("~/Images/HomeForSale/Picture/") + homeforsale.Picture2.ToString();
            }
            else
            {
                Picture2Location = ("~/Images/HomeForSale/Picture/bullet.png");
            }

            ViewBag.Picture2 = Picture2Location;

            if (homeforsale.Picture3 != null)
            {
                Picture3Location = ("~/Images/HomeForSale/Picture/") + homeforsale.Picture3.ToString();
            }
            else
            {
                Picture3Location = ("~/Images/HomeForSale/Picture/bullet.png");
            }

            ViewBag.Picture3 = Picture3Location;

            if (homeforsale.Picture4 != null)
            {
                Picture4Location = ("~/Images/HomeForSale/Picture/") + homeforsale.Picture4.ToString();
            }
            else
            {
                Picture4Location = ("~/Images/HomeForSale/Picture/bullet.png");
            }

            ViewBag.Picture4 = Picture4Location;

            if (homeforsale.Picture5 != null)
            {
                Picture5Location = ("~/Images/HomeForSale/Picture/") + homeforsale.Picture5.ToString();
            }
            else
            {
                Picture5Location = ("~/Images/HomeForSale/Picture/bullet.png");
            }

            ViewBag.Picture5 = Picture5Location;

            if (homeforsale.Movie != null)
            {
                MovieLocation = ("~/Images/HomeForSale/Movie/") + homeforsale.Movie.ToString();
            }
            else
            {
                MovieLocation = ("");
            }

            ViewBag.Movie = MovieLocation;

            return View(homeforsale);
        }

        //
        // GET: /HomeForSale/Create

        [Authorize(Roles = "RealEstateAgent")]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /HomeForSale/Create

        [HttpPost]
        [Authorize(Roles = "RealEstateAgent")]
        //[ValidateAntiForgeryToken]
        public ActionResult Create(HomeForSale homeforsale, HttpPostedFileBase MainPicture, HttpPostedFileBase Picture1, HttpPostedFileBase Picture2, HttpPostedFileBase Picture3, HttpPostedFileBase Picture4, HttpPostedFileBase Picture5, HttpPostedFileBase Movie)
        {
            if (ModelState.IsValid)
            {

                String CreateQRCode = Request.Form["chkQRCode"];

                //Save MainPicture if file exists

                if (MainPicture != null)
                {
                    Guid guidMainPicture = Guid.NewGuid();

                    string imageLocation = Server.MapPath("~/Images/HomeForSale/Picture/") + guidMainPicture.ToString() + ".jpg";

                    MainPicture.SaveAs(imageLocation);

                    homeforsale.MainPicture = guidMainPicture.ToString() + ".jpg";
                }

                //Save Picture1 if file exists

                if (Picture1 != null)
                {
                    Guid guidPicture1 = Guid.NewGuid();

                    string imageLocation = Server.MapPath("~/Images/HomeForSale/Picture/") + guidPicture1.ToString() + ".jpg";

                    Picture1.SaveAs(imageLocation);

                    homeforsale.Picture1 = guidPicture1.ToString() + ".jpg";
                }

                //Save Picture2 if file exists

                if (Picture2 != null)
                {
                    Guid guidPicture2 = Guid.NewGuid();

                    string imageLocation = Server.MapPath("~/Images/HomeForSale/Picture/") + guidPicture2.ToString() + ".jpg";

                    Picture2.SaveAs(imageLocation);

                    homeforsale.Picture2 = guidPicture2.ToString() + ".jpg";
                }

                //Save Picture3 if file exists

                if (Picture3 != null)
                {
                    Guid guidPicture3 = Guid.NewGuid();

                    string imageLocation = Server.MapPath("~/Images/HomeForSale/Picture/") + guidPicture3.ToString() + ".jpg";

                    Picture3.SaveAs(imageLocation);

                    homeforsale.Picture3 = guidPicture3.ToString() + ".jpg";
                }

                //Save Picture4 if file exists

                if (Picture4 != null)
                {
                    Guid guidPicture4 = Guid.NewGuid();

                    string imageLocation = Server.MapPath("~/Images/HomeForSale/Picture/") + guidPicture4.ToString() + ".jpg";

                    Picture4.SaveAs(imageLocation);

                    homeforsale.Picture4 = guidPicture4.ToString() + ".jpg";
                }

                //Save Picture5 if file exists

                if (Picture5 != null)
                {
                    Guid guidPicture5 = Guid.NewGuid();

                    string imageLocation = Server.MapPath("~/Images/HomeForSale/Picture/") + guidPicture5.ToString() + ".jpg";

                    Picture5.SaveAs(imageLocation);

                    homeforsale.Picture5 = guidPicture5.ToString() + ".jpg";
                }

                //Save Movie if file exists
                if (Movie != null)
                {
                    Guid guidMovie = Guid.NewGuid();

                    string movieLocation = Server.MapPath("~/Images/HomeForSale/Movie/") + guidMovie.ToString() + ".wmv";

                    Movie.SaveAs(movieLocation);

                    homeforsale.Movie = guidMovie.ToString() + ".wmv";
                }

                homeforsale.AgentUserName = User.Identity.Name;

                db.HomeForSales.Add(homeforsale);
                db.SaveChanges();

                //Add record inside HomeQRCOde

                HomeQRCode homeQRCode = new HomeQRCode();
                homeQRCode.HomeForSaleID = homeforsale.ID;
                dbQR.HomeQRCodes.Add(homeQRCode);
                dbQR.SaveChanges();

                if (CreateQRCode == "true")
                    CreateQRCodeHome(homeforsale, homeQRCode);


                return RedirectToAction("Index");
            }

            return View(homeforsale);
        }

        //
        // GET: /HomeForSale/Edit/5

        public ActionResult Edit(int id = 0)
        {
            HomeForSale homeforsale = db.HomeForSales.Find(id);
            if (homeforsale == null)
            {
                return HttpNotFound();
            }

            string LogoLocation = ("~/Images/CompanyLogo.jpg");


            var homeQRCode = dbQR.HomeQRCodes.Where(b => b.HomeForSaleID == homeforsale.ID).ToList()[0];
            string QRCodeLocation = "";
            string FlyerLocation = "";


            if (homeQRCode.QRCode != null)
            {
                ViewBag.QRCodeExists = "Yes";
                QRCodeLocation = ("~/Images/HomeForSale/QRCode/") + homeQRCode.QRCode.ToString();
                FlyerLocation = ("~/Images/HomeForSale/QRCode/") + homeQRCode.Flyer.ToString();
            }
            else
            {
                ViewBag.QRCodeExists = "No";
            }

            ViewBag.QRCodeLocation = QRCodeLocation;
            ViewBag.FlyerLocation = FlyerLocation;


            string MainPictureLocation = "";
            string Picture1Location = "";
            string Picture2Location = "";
            string Picture3Location = "";
            string Picture4Location = "";
            string Picture5Location = "";
            string MovieLocation = "";

            if (homeforsale.MainPicture != null)
            {
                MainPictureLocation = ("~/Images/HomeForSale/Picture/") + homeforsale.MainPicture.ToString();
            }
            else
            {
                MainPictureLocation = ("~/Images/HomeForSale/Picture/bullet.png");
            }

            ViewBag.MainPicture = MainPictureLocation;

            if (homeforsale.Picture1 != null)
            {
                Picture1Location = ("~/Images/HomeForSale/Picture/") + homeforsale.Picture1.ToString();
            }
            else
            {
                Picture1Location = ("~/Images/HomeForSale/Picture/bullet.png");
            }

            ViewBag.Picture1 = Picture1Location;

            if (homeforsale.Picture2 != null)
            {
                Picture2Location = ("~/Images/HomeForSale/Picture/") + homeforsale.Picture2.ToString();
            }
            else
            {
                Picture2Location = ("~/Images/HomeForSale/Picture/bullet.png");
            }

            ViewBag.Picture2 = Picture2Location;

            if (homeforsale.Picture3 != null)
            {
                Picture3Location = ("~/Images/HomeForSale/Picture/") + homeforsale.Picture3.ToString();
            }
            else
            {
                Picture3Location = ("~/Images/HomeForSale/Picture/bullet.png");
            }

            ViewBag.Picture3 = Picture3Location;

            if (homeforsale.Picture4 != null)
            {
                Picture4Location = ("~/Images/HomeForSale/Picture/") + homeforsale.Picture4.ToString();
            }
            else
            {
                Picture4Location = ("~/Images/HomeForSale/Picture/bullet.png");
            }

            ViewBag.Picture4 = Picture4Location;

            if (homeforsale.Picture5 != null)
            {
                Picture5Location = ("~/Images/HomeForSale/Picture/") + homeforsale.Picture5.ToString();
            }
            else
            {
                Picture5Location = ("~/Images/HomeForSale/Picture/bullet.png");
            }

            ViewBag.Picture5 = Picture5Location;

            if (homeforsale.Movie != null)
            {
                MovieLocation = ("~/Images/HomeForSale/Movie/") + homeforsale.Movie.ToString();
            }
            else
            {
                MovieLocation = ("~/Images/HomeForSale/Movie/bullet.png");
            }

            ViewBag.Movie = MovieLocation;

            return View(homeforsale);
        }

        //
        // POST: /HomeForSale/Edit/5

        [HttpPost]
        [Authorize(Roles = "RealEstateAgent")]
        //[ValidateAntiForgeryToken]
        public ActionResult Edit(HomeForSale homeforsale, HttpPostedFileBase MainPicture, HttpPostedFileBase Picture1, HttpPostedFileBase Picture2, HttpPostedFileBase Picture3, HttpPostedFileBase Picture4, HttpPostedFileBase Picture5, HttpPostedFileBase Movie)
        {
            if (ModelState.IsValid)
            {
                String MainPictureForm = Request.Form["MainPicture"];
                String Picture1Form = Request.Form["Picture1"];
                String Picture2Form = Request.Form["Picture2"];
                String Picture3Form = Request.Form["Picture3"];
                String Picture4Form = Request.Form["Picture4"];
                String Picture5Form = Request.Form["Picture5"];
                String MovieForm = Request.Form["Movie"];

                //Check if QRCode exists in database
                var homeQRCode = dbQR.HomeQRCodes.Where(b => b.HomeForSaleID == homeforsale.ID).ToList()[0];
                
                String CreateQRCode = Request.Form["chkQRCode"];

                //Save picture if file exists

                if (MainPicture != null)
                {
                    Guid guid = Guid.NewGuid();

                    string imageLocation = Server.MapPath("~/Images/HomeForSale/Picture/") + guid.ToString() + ".jpg";

                    MainPicture.SaveAs(imageLocation);

                    homeforsale.MainPicture = guid.ToString() + ".jpg";
                }
                else
                {
                    homeforsale.MainPicture = MainPictureForm;
                }

                if (Picture1 != null)
                {
                    Guid guid = Guid.NewGuid();

                    string imageLocation = Server.MapPath("~/Images/HomeForSale/Picture/") + guid.ToString() + ".jpg";

                    Picture1.SaveAs(imageLocation);

                    homeforsale.Picture1 = guid.ToString() + ".jpg";
                }
                else
                {
                    homeforsale.Picture1 = Picture1Form;
                }

                if (Picture2 != null)
                {
                    Guid guid = Guid.NewGuid();

                    string imageLocation = Server.MapPath("~/Images/HomeForSale/Picture/") + guid.ToString() + ".jpg";

                    Picture2.SaveAs(imageLocation);

                    homeforsale.Picture2 = guid.ToString() + ".jpg";
                }
                else
                {
                    homeforsale.Picture2 = Picture2Form;
                }

                if (Picture3 != null)
                {
                    Guid guid = Guid.NewGuid();

                    string imageLocation = Server.MapPath("~/Images/HomeForSale/Picture/") + guid.ToString() + ".jpg";

                    Picture3.SaveAs(imageLocation);

                    homeforsale.Picture3 = guid.ToString() + ".jpg";
                }
                else
                {
                    homeforsale.Picture3 = Picture3Form;
                }

                if (Picture4 != null)
                {
                    Guid guid = Guid.NewGuid();

                    string imageLocation = Server.MapPath("~/Images/HomeForSale/Picture/") + guid.ToString() + ".jpg";

                    Picture4.SaveAs(imageLocation);

                    homeforsale.Picture4 = guid.ToString() + ".jpg";
                }
                else
                {
                    homeforsale.Picture4 = Picture4Form;
                }

                if (Picture5 != null)
                {
                    Guid guid = Guid.NewGuid();

                    string imageLocation = Server.MapPath("~/Images/HomeForSale/Picture/") + guid.ToString() + ".jpg";

                    Picture5.SaveAs(imageLocation);

                    homeforsale.Picture5 = guid.ToString() + ".jpg";
                }
                else
                {
                    homeforsale.Picture5 = Picture5Form;
                }

                if (Movie != null)
                {
                    Guid guid = Guid.NewGuid();

                    string movieLocation = Server.MapPath("~/Images/HomeForSale/Movie/") + guid.ToString() + ".jpg";

                    Movie.SaveAs(movieLocation);

                    homeforsale.Movie = guid.ToString() + ".jpg";
                }
                else
                {
                    homeforsale.Movie = MovieForm;
                }


                db.Entry(homeforsale).State = EntityState.Modified;
                db.SaveChanges();

                //
                if ((homeQRCode.QRCode == null) && (CreateQRCode == "true"))
                {
                    CreateQRCodeHome(homeforsale, homeQRCode);

                    /*
                    Guid guid = Guid.NewGuid();

                    string googleChartUrl = "http://chart.apis.google.com/chart?cht=qr&chs=300x300&chl=http://www.qrcode-coupons.com/Coupon/Create/?homeqr=" + guid.ToString();

                    string imageLocation = Server.MapPath("~/Images/HomeForSale/QRCode/") + guid.ToString() + ".jpg";

                    WebClient webClient = new WebClient();
                    webClient.DownloadFile(googleChartUrl, imageLocation);

                    homeQRCode.Code = guid.ToString();
                    homeQRCode.QRCode = guid.ToString() + ".jpg";

                    //Create Flyer Home

                    Image mpImg = null;

                    //Add Main picture 
                    if (homeforsale.MainPicture != null)
                    {
                        string mainPictureLocation = Server.MapPath("~/Images/HomeForSale/Picture/") + homeforsale.MainPicture;
                        mpImg = Image.FromFile(mainPictureLocation);
                    }

                    //Agent picture
                    var users = dbUser.Users.Where(b => b.UserName == User.Identity.Name).ToList();
                    User user = users[0];

                    string imageUserLocation = Server.MapPath("~/Images/User/") + user.Picture.ToString();
                    Image UImg = Image.FromFile(imageUserLocation);

                    //QRCode
                    Bitmap original = (Bitmap)Image.FromFile(imageLocation);
                    Bitmap resized = new Bitmap(original, new Size(90, 90));

                    Guid guidFL = Guid.NewGuid();
                    string flLocation = Server.MapPath("~/Images/FlyerHome.png");

                    Image FLImg = Image.FromFile(flLocation);

                    Graphics flagGraphicsFL = Graphics.FromImage(FLImg);

                    if (mpImg != null)
                        flagGraphicsFL.DrawImage(mpImg, 40, 100);

                    flagGraphicsFL.DrawImage(UImg, 15, 540);
                    flagGraphicsFL.DrawImage(resized, 480, 530);

                    string font = "Arial";

                    flagGraphicsFL.DrawString(homeforsale.Description, new Font(font, 12, FontStyle.Regular), new SolidBrush(Color.FromArgb(254, Color.Black)), new Point(50, 70));
                    flagGraphicsFL.DrawString(homeforsale.AskingPrice + "$", new Font(font, 12, FontStyle.Regular), new SolidBrush(Color.FromArgb(254, Color.Red)), new Point(240, 440));
                    flagGraphicsFL.DrawString(homeforsale.Address, new Font(font, 12, FontStyle.Regular), new SolidBrush(Color.FromArgb(254, Color.Black)), new Point(200, 460));


                    flagGraphicsFL.DrawString(user.FirstName + " " + user.LastName, new Font(font, 12, FontStyle.Regular), new SolidBrush(Color.FromArgb(254, Color.Black)), new Point(99, 540));
                    flagGraphicsFL.DrawString("Sales Representative", new Font(font, 8, FontStyle.Regular), new SolidBrush(Color.FromArgb(254, Color.Red)), new Point(100, 560));
                    flagGraphicsFL.DrawString("Phone : " + user.PhoneNumber, new Font(font, 8, FontStyle.Regular), new SolidBrush(Color.FromArgb(254, Color.Black)), new Point(100, 585));
                    flagGraphicsFL.DrawString("Email : " + user.Email, new Font(font, 8, FontStyle.Regular), new SolidBrush(Color.FromArgb(254, Color.Black)), new Point(100, 595));


                    flLocation = Server.MapPath("~/Images/HomeForSale/QRCode/") + guidFL.ToString() + ".png";
                    FLImg.Save(flLocation);

                    homeQRCode.Flyer = guidFL.ToString() + ".png";

                    dbQR.Entry(homeQRCode).State = EntityState.Modified;
                    dbQR.SaveChanges();
                    
                    */

                }


                return RedirectToAction("Index");
            }
            return View(homeforsale);
        }

        public void CreateQRCodeHome(HomeForSale homeforsale, HomeQRCode homeQRCode)
        {
            Guid guid = Guid.NewGuid();

            Company company = dbCompany.Companies.Find(1);

            string googleChartUrl = "http://chart.apis.google.com/chart?cht=qr&chs=300x300&chl=" + company.Domain + "/HomeForSale/View/?homeqr=" + guid.ToString();

            string imageLocation = Server.MapPath("~/Images/HomeForSale/QRCode/") + guid.ToString() + ".jpg";

            WebClient webClient = new WebClient();
            webClient.DownloadFile(googleChartUrl, imageLocation);

            homeQRCode.Code = guid.ToString();
            homeQRCode.QRCode = guid.ToString() + ".jpg";

            //Create Flyer Home

            Image mpImg = null;

            //Add Main picture 
            if (homeforsale.MainPicture != null)
            {
                string mainPictureLocation = Server.MapPath("~/Images/HomeForSale/Picture/") + homeforsale.MainPicture;
                mpImg = Image.FromFile(mainPictureLocation);
            }

            //Agent picture
            var users = dbUser.Users.Where(b => b.UserName == User.Identity.Name).ToList();
            User user = users[0];

            string imageUserLocation = Server.MapPath("~/Images/User/") + user.Picture.ToString();
            Image UImg = Image.FromFile(imageUserLocation);

            //QRCode
            Bitmap original = (Bitmap)Image.FromFile(imageLocation);
            Bitmap resized = new Bitmap(original, new Size(90, 90));

            Guid guidFL = Guid.NewGuid();
            string flLocation = Server.MapPath("~/Images/FlyerHome.png");

            Image FLImg = Image.FromFile(flLocation);

            Graphics flagGraphicsFL = Graphics.FromImage(FLImg);

            if (mpImg != null)
                flagGraphicsFL.DrawImage(mpImg, 40, 100);

            flagGraphicsFL.DrawImage(UImg, 15, 540);
            flagGraphicsFL.DrawImage(resized, 480, 530);

            string font = "Arial";

            flagGraphicsFL.DrawString(homeforsale.Description, new Font(font, 12, FontStyle.Regular), new SolidBrush(Color.FromArgb(254, Color.Black)), new Point(50, 70));
            flagGraphicsFL.DrawString(homeforsale.AskingPrice + "$", new Font(font, 12, FontStyle.Regular), new SolidBrush(Color.FromArgb(254, Color.Red)), new Point(240, 440));
            flagGraphicsFL.DrawString(homeforsale.Address, new Font(font, 12, FontStyle.Regular), new SolidBrush(Color.FromArgb(254, Color.Black)), new Point(200, 460));


            flagGraphicsFL.DrawString(user.FirstName + " " + user.LastName, new Font(font, 12, FontStyle.Regular), new SolidBrush(Color.FromArgb(254, Color.Black)), new Point(99, 540));
            flagGraphicsFL.DrawString("Sales Representative", new Font(font, 8, FontStyle.Regular), new SolidBrush(Color.FromArgb(254, Color.Red)), new Point(100, 560));
            flagGraphicsFL.DrawString("Phone : " + user.PhoneNumber, new Font(font, 8, FontStyle.Regular), new SolidBrush(Color.FromArgb(254, Color.Black)), new Point(100, 585));
            flagGraphicsFL.DrawString("Email : " + user.Email, new Font(font, 8, FontStyle.Regular), new SolidBrush(Color.FromArgb(254, Color.Black)), new Point(100, 595));


            flLocation = Server.MapPath("~/Images/HomeForSale/QRCode/") + guidFL.ToString() + ".png";
            FLImg.Save(flLocation);

            homeQRCode.Flyer = guidFL.ToString() + ".png";

            dbQR.Entry(homeQRCode).State = EntityState.Modified;
            dbQR.SaveChanges();
        }

        //
        // GET: /HomeForSale/Delete/5

        public ActionResult Delete(int id = 0)
        {
            HomeForSale homeforsale = db.HomeForSales.Find(id);
            if (homeforsale == null)
            {
                return HttpNotFound();
            }
            return View(homeforsale);
        }

        //
        // POST: /HomeForSale/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            HomeForSale homeforsale = db.HomeForSales.Find(id);
            db.HomeForSales.Remove(homeforsale);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}